

/* Marginal treatment effects */


cd G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Outputs\Revision\FINAL


use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv", replace


keep if time==1

egen lawyer_id_unique =group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts
bys lawyer_id_unique: egen n_lawyer=count(lawyer_id_unique)
egen mean_lawyer_leniency= mean(granted), by(lawyer_id_unique)

gen iv_judge2_all = (mean_lawyer_leniency*n_lawyer - granted)/(n_lawyer-1)

gen iv_lawyer2=iv_judge2_all-(iv_court-granted)/(obs_court-1)


keep if n_lawyer>=20

egen n_judge=nvals(lawyer_id_unique), by(court aar)



tab n_judge

keep if n_judge>1

keep pnr iv_lawyer2 lawyer_id_unique


merge 1:m pnr using "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv"

keep if _merge==3
drop _merge



keep if time>0

tab lawyer_id_unique, gen(lawyer_dum)

egen court_pyear =group(court pyear) 
tab court_pyear

tab time

tab aar, gen(aardum)



/* MTE */

local setlist "standard separate mlikelihood fullsupport semiparametric"

local setlist "separate semiparametric"

local setlist "standard"


local depvar erhvervsindk_real 

	
foreach setting of local setlist {
		
	if "`setting'" == "standard" {
			
	global spec = ""
			
	}
	else {
			
	global spec = "`setting'"
			
	}
		

	* Earnings
	replace erhvervsindk_real=erhvervsindk_real/1000
	local depvar erhvervsindk_real 

	
	foreach var of varlist `depvar' {		

		local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"

		mtefe `var' (granted=iv_lawyer2) `Xvar_short' i.court_pyear, sep link(logit)  polynomial(2)  trimsupport(0.05)  vce(cluster lawyer_id_unique) 
		
		graph export ComSup_`var'_05_sep_poly2, as(pdf) name(CommonSupport) replace

			
		mtefeplot , graph_opts(ylabel(  -25 0 25 50  75 100 ,nogrid angle(0) labsize(medsmall))  title("") graphregion(fcolor(white)) bgcolor(white) ytitle("Earned Income", size(medium))  )
 graph save `var', replace
			graph export mte_`var'_05_sep_poly2, as(pdf)   replace

			
		mat b = e(b)

		local names: colfullnames b
		local newnames

		foreach name of local names {
			local newnames `newnames' `=strtoname("`name'")'	
		}

		matrix colnames b = `newnames'

		preserve

		svmat b, names(col)

		keep if _n == 1
		keep effects*

		export excel using "MTE_`setting'_05_sep_poly2.xlsx", firstrow(variables) sheet("`var'", replace)

		restore

		putexcel set MTE_`setting'_05_sep_poly2, sheet("`var'") modify

		putexcel B5 = "`e(N)'"
		putexcel B6 = "`e(df_r)'"
		putexcel B7 = "`e(iv)'"

		putexcel A5 = "Observations"
		putexcel A6 = "df_r"
		putexcel A7 = "IV"
			
	
	}
	
	
	
	}
	
* Employment



local depvar  employed_pstill 

	
foreach setting of local setlist {
		
	if "`setting'" == "standard" {
			
	global spec = ""
			
	}
	else {
			
	global spec = "`setting'"
			
	}
		

	local depvar  employed_pstill 

	
	foreach var of varlist `depvar' {		

		local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"

		mtefe `var' (granted=iv_lawyer2) `Xvar_short' i.court_pyear, sep link(logit)  polynomial(2)  trimsupport(0.05)  vce(cluster lawyer_id_unique) 
		
		graph export ComSup_`var'_05_sep_poly2, as(pdf) name(CommonSupport) replace

			
		mtefeplot , graph_opts( ylabel( -0.1  0 0.1  0.2  ,nogrid angle(0) labsize(medsmall))  title("") ytitle("Employment", size(medium)))
graph save `var', replace
			graph export mte_`var'_05_sep_poly2, as(pdf)   replace

			
		mat b = e(b)

		local names: colfullnames b
		local newnames

		foreach name of local names {
			local newnames `newnames' `=strtoname("`name'")'	
		}

		matrix colnames b = `newnames'

		preserve

		svmat b, names(col)

		keep if _n == 1
		keep effects*

		export excel using "MTE_`setting'_05_sep_poly2.xlsx", firstrow(variables) sheet("`var'", replace)

		restore

		putexcel set MTE_`setting'_05_sep_poly2, sheet("`var'") modify

		putexcel B5 = "`e(N)'"
		putexcel B6 = "`e(df_r)'"
		putexcel B7 = "`e(iv)'"

		putexcel A5 = "Observations"
		putexcel A6 = "df_r"
		putexcel A7 = "IV"
			
	
	}
	
	
	
	}	

	 grc1leg erhvervsindk_real.gph employed_pstill.gph , graphregion(fcolor(white)) scheme(s1color) cols(2) rows(1) imargin(small)
graph export MTE_combined.pdf, replace


	


	
foreach setting of local setlist {
		
	if "`setting'" == "standard" {
			
	global spec = ""
			
	}
	else {
			
	global spec = "`setting'"
			
	}
		
	
cd G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Outputs\Revision\FINAL
	

	* Assets
	
	local depvar  aktiv_real
replace aktiv_real=aktiv_real/1000 
	
	foreach var of varlist `depvar' {		

		local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"

		mtefe `var' (granted=iv_lawyer2) `Xvar_short' i.court_pyear, sep link(logit)  polynomial(2)  trimsupport(0.05)  vce(cluster lawyer_id_unique) 
		
		graph export ComSup_`var'_05_sep_poly2, as(pdf) name(CommonSupport) replace

			
		mtefeplot , graph_opts( ylabel(100 200 300 400 500,nogrid angle(0) labsize(medsmall))  title("") ///
ytitle("Assets", size(medium)))
graph save `var', replace
 
			graph export mte_`var'_05_sep_poly2, as(pdf)   replace

			
		mat b = e(b)

		local names: colfullnames b
		local newnames

		foreach name of local names {
			local newnames `newnames' `=strtoname("`name'")'	
		}

		matrix colnames b = `newnames'

		preserve

		svmat b, names(col)

		keep if _n == 1
		keep effects*

		export excel using "MTE_`setting'_05_sep_poly2.xlsx", firstrow(variables) sheet("`var'", replace)

		restore


		putexcel set MTE_`setting'_05_sep_poly2, sheet("`var'") modify

		putexcel B5 = "`e(N)'"
		putexcel B6 = "`e(df_r)'"
		putexcel B7 = "`e(iv)'"

		putexcel A5 = "Observations"
		putexcel A6 = "df_r"
		putexcel A7 = "IV"
			
	
	}
	
	
	
	}	
	
* Formue
	local depvar formue_real 
replace formue_real=formue_real/1000
	
	foreach var of varlist `depvar' {		

		local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"

		mtefe `var' (granted=iv_lawyer2) `Xvar_short' i.court_pyear, sep link(logit)  polynomial(2)  trimsupport(0.05)  vce(cluster lawyer_id_unique) 
		
		graph export ComSup_`var'_05_sep_poly2, as(pdf) name(CommonSupport) replace

			
		mtefeplot , graph_opts( ylabel(100 200 300 400,nogrid angle(0) labsize(medsmall))  title("") ///
ytitle("Wealth", size(medium)))
graph save `var', replace
 
			graph export mte_`var'_05_sep_poly2, as(pdf)   replace

			
		mat b = e(b)

		local names: colfullnames b
		local newnames

		foreach name of local names {
			local newnames `newnames' `=strtoname("`name'")'	
		}

		matrix colnames b = `newnames'

		preserve

		svmat b, names(col)

		keep if _n == 1
		keep effects*

		export excel using "MTE_`setting'_05_sep_poly2.xlsx", firstrow(variables) sheet("`var'", replace)

		restore


		putexcel set MTE_`setting'_05_sep_poly2, sheet("`var'") modify

		putexcel B5 = "`e(N)'"
		putexcel B6 = "`e(df_r)'"
		putexcel B7 = "`e(iv)'"

		putexcel A5 = "Observations"
		putexcel A6 = "df_r"
		putexcel A7 = "IV"
			
	
	}
	
	
	
			
	
	
	
	* Secured Debt
	
		local depvar mortgage_real 
replace mortgage_real=mortgage_real/1000
	
	foreach var of varlist `depvar' {		

		local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"

		mtefe `var' (granted=iv_lawyer2) `Xvar_short' i.court_pyear, sep link(logit)  polynomial(2)  trimsupport(0.05)  vce(cluster lawyer_id_unique) 
		
		graph export ComSup_`var'_05_sep_poly2, as(pdf) name(CommonSupport) replace

			
		mtefeplot , graph_opts( ylabel(0 100 200 300 ,nogrid angle(0) labsize(medsmall))  title("") ytitle("Secured Debt", size(medium)))
 
			graph export mte_`var'_05_sep_poly2, as(pdf)   replace
graph save `var', replace
			
		mat b = e(b)

		local names: colfullnames b
		local newnames

		foreach name of local names {
			local newnames `newnames' `=strtoname("`name'")'	
		}

		matrix colnames b = `newnames'

		preserve

		svmat b, names(col)

		keep if _n == 1
		keep effects*

		export excel using "MTE_`setting'_05_sep_poly2.xlsx", firstrow(variables) sheet("`var'", replace)

		restore


		putexcel set MTE_`setting'_05_sep_poly2, sheet("`var'") modify

		putexcel B5 = "`e(N)'"
		putexcel B6 = "`e(df_r)'"
		putexcel B7 = "`e(iv)'"

		putexcel A5 = "Observations"
		putexcel A6 = "df_r"
		putexcel A7 = "IV"
			
	
	}
	
	
	
	
	
	* Unsecured Debt
	
	local depvar  bankgaeld_real 
	replace bankgaeld_real=bankgaeld_real/1000

	
	foreach var of varlist `depvar' {		

		local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"

		mtefe `var' (granted=iv_lawyer2) `Xvar_short' i.court_pyear, sep link(logit)  polynomial(2)  trimsupport(0.05)  vce(cluster lawyer_id_unique) 
		
		graph export ComSup_`var'_05_sep_poly2, as(pdf) name(CommonSupport) replace

		mtefeplot , graph_opts( ylabel(-300 -200 -100 0,nogrid angle(0) labsize(medsmall))  title("") ytitle("Unsecured Debt", size(medium)))
 graph save `var', replace
			graph export mte_`var'_05_sep_poly2, as(pdf)   replace

			
		mat b = e(b)

		local names: colfullnames b
		local newnames

		foreach name of local names {
			local newnames `newnames' `=strtoname("`name'")'	
		}

		matrix colnames b = `newnames'

		preserve

		svmat b, names(col)

		keep if _n == 1
		keep effects*

		export excel using "MTE_`setting'_05_sep_poly2.xlsx", firstrow(variables) sheet("`var'", replace)

		restore


		putexcel set MTE_`setting'_05_sep_poly2, sheet("`var'") modify

		putexcel B5 = "`e(N)'"
		putexcel B6 = "`e(df_r)'"
		putexcel B7 = "`e(iv)'"

		putexcel A5 = "Observations"
		putexcel A6 = "df_r"
		putexcel A7 = "IV"
			
	
	}
	
	
	
	
	

/*

PLOT APPENDIX MATERIAL
*/




	 grc1leg erhvervsindk_real.gph employed_pstill.gph aktiv_real.gph formue_real.gph ///
	bankgaeld_real.gph mortgage_real.gph, graphregion(fcolor(white)) scheme(s1color) cols(2) rows(3) imargin(small) 
graph export MTE_combined_6_outcomes.pdf, replace




cd G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Outputs\Revision\FINAL


use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv", replace


keep if time==1

egen lawyer_id_unique =group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts
bys lawyer_id_unique: egen n_lawyer=count(lawyer_id_unique)
egen mean_lawyer_leniency= mean(granted), by(lawyer_id_unique)

gen iv_judge2_all = (mean_lawyer_leniency*n_lawyer - granted)/(n_lawyer-1)

gen iv_lawyer2=iv_judge2_all-(iv_court-granted)/(obs_court-1)



keep if n_lawyer>=20

egen n_judge=nvals(lawyer_id_unique), by(court aar)



tab n_judge

keep if n_judge>1

keep pnr iv_lawyer2 lawyer_id_unique


merge 1:m pnr using "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv"

keep if _merge==3
drop _merge



keep if time>0

tab lawyer_id_unique, gen(lawyer_dum)

egen court_pyear =group(court pyear) 
tab court_pyear

tab time

tab aar, gen(aardum)


/* MTE */

local setlist "standard separate mlikelihood fullsupport semiparametric"

local setlist "separate semiparametric"

local setlist "standard"

replace erhvervsindk_real=erhvervsindk_real/1000

local depvar erhvervsindk_real

	
foreach setting of local setlist {
		
	if "`setting'" == "standard" {
			
	global spec = ""
			
	}
	else {
			
	global spec = "`setting'"
			
	}
		
	cd G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Outputs\Revision\FINAL


	foreach var of varlist `depvar' {		

		local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"

		mtefe `var' (granted=iv_lawyer2) `Xvar_short' i.court_pyear, link(logit)  sep polynomial(3)  trimsupport(0.05)  vce(cluster lawyer_id_unique)
eststo sep_poly3
		mtefe `var' (granted=iv_lawyer2) `Xvar_short' i.court_pyear, link(logit)  polynomial(2)  trimsupport(0.05)  vce(cluster lawyer_id_unique)
		eststo poly2
	
		mtefe `var' (granted=iv_lawyer2) `Xvar_short' i.court_pyear, link(logit)  sep polynomial(2)  trimsupport(0.01)  vce(cluster lawyer_id_unique)
		eststo sep_trim1
		
			mtefe `var' (granted=iv_lawyer2) `Xvar_short' i.court_pyear, link(logit)  polynomial(2) sep  trimsupport(0.05)  vce(cluster lawyer_id_unique)
		eststo sep
	
		mtefe `var' (granted=iv_lawyer2) `Xvar_short' i.court_pyear, semiparametric gridpoints(100) trimsupport(0.05)  vce(cluster lawyer_id_unique)
	eststo semi	
	
	
	
mtefeplot sep sep_poly3 poly2 sep_trim1 semi, memory legendtitle("Specification") names("1" "2" "3" "4" "5")  graph_opts( ylabel(-100 0 100 200 ,nogrid angle(0) labsize(medsmall))  title("") ytitle("Earned Income", size(medium)))
 graph save `var'_combined, replace
			graph export mte_`var'_together, as(pdf)  replace

		
		
	}
}


* Employment

local depvar  employed_pstill 

	
foreach setting of local setlist {
		
	if "`setting'" == "standard" {
			
	global spec = ""
			
	}
	else {
			
	global spec = "`setting'"
			
	}
		
cd G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Outputs\Revision\FINAL

	foreach var of varlist `depvar' {		

		local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"

		mtefe `var' (granted=iv_lawyer2) `Xvar_short' i.court_pyear, link(logit) polynomial(3)  sep  trimsupport(0.05)  vce(cluster lawyer_id_unique)
eststo sep_poly3
		mtefe `var' (granted=iv_lawyer2) `Xvar_short' i.court_pyear, link(logit)  polynomial(2)  trimsupport(0.05)  vce(cluster lawyer_id_unique)
		eststo poly2
	
		mtefe `var' (granted=iv_lawyer2) `Xvar_short' i.court_pyear, link(logit)  sep polynomial(2)  trimsupport(0.01)  vce(cluster lawyer_id_unique)
		eststo sep_trim1
		
			mtefe `var' (granted=iv_lawyer2) `Xvar_short' i.court_pyear, link(logit)  polynomial(2) sep  trimsupport(0.05)  vce(cluster lawyer_id_unique)
		eststo sep
	
		mtefe `var' (granted=iv_lawyer2) `Xvar_short' i.court_pyear, semiparametric gridpoints(100) trimsupport(0.05)  vce(cluster lawyer_id_unique)
	eststo semi	

	
mtefeplot sep sep_poly3 poly2 sep_trim1 semi, memory legendtitle("Specification") names("1" "2" "3" "4" "5")  graph_opts( ylabel(-0.1 0 0.1 0.2 0.3 ,nogrid angle(0) labsize(medsmall))  title("") ytitle("Employment", size(medium)))
		
 graph save `var'_combined, replace

			graph export mte_`var'_together, as(pdf)  replace

		
		
	}
}



	 grc1leg erhvervsindk_real_combined.gph employed_pstill_combined.gph, graphregion(fcolor(white)) scheme(s1color) cols(2) rows(1) imargin(small) 
graph export MTE_combined_employ_income.pdf, replace
